package util;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Locale;

/**
 * Byte转换工具
 *
 * @author bingo
 */
public class ByteUtil {

    /**
     * 十六进制字符串转byte[]
     *
     * @param hex 十六进制字符串
     * @return byte[]
     */
    public static byte[] hexStr2Byte(String hex) {
        if (hex == null) {
            return new byte[] {};
        }

        // 奇数位补0
        if (hex.length() % 2 != 0) {
            hex = "0" + hex;
        }

        int length = hex.length();
        ByteBuffer buffer = ByteBuffer.allocate(length / 2);
        for (int i = 0; i < length; i++) {
            String hexStr = hex.charAt(i) + "";
            i++;
            hexStr += hex.charAt(i);
            byte b = (byte) Integer.parseInt(hexStr, 16);
            buffer.put(b);
        }
        return buffer.array();
    }

    /**
     * byte[]转十六进制字符串
     *
     * @param array
     *            byte[]
     * @return 十六进制字符串
     */
    public static String byteArrayToHexString(byte[] array) {
        if (array == null) {
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            buffer.append(byteToHex(array[i]));
        }
        return buffer.toString();
    }

    /**
     * byte转十六进制字符
     *
     * @param b
     *            byte
     * @return 十六进制字符
     */
    public static String byteToHex(byte b) {
        String hex = Integer.toHexString(b & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        return hex.toUpperCase(Locale.getDefault());
    }

    public static void main(String[] args) {
        System.out.println(Integer.toString(5,8));
        System.out.println(Integer.toString(9,8));
     /*   String  msg  = "01030000000585";
        System.out.println(Arrays.toString(msg.getBytes()));

        System.out.println(Arrays.toString(ByteUtil.hexStr2Byte(msg)));
        //36    24
        System.out.println(Arrays.toString("36".getBytes()));
        System.out.println(Arrays.toString(ByteUtil.hexStr2Byte("24")));*/

    }
}
